package com.zzyl.nursing.service.impl;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.List;

import cn.hutool.core.date.LocalDateTimeUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.zzyl.common.utils.DateUtils;
import com.zzyl.common.utils.UserThreadLocal;
import com.zzyl.nursing.vo.TimeCountVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zzyl.nursing.mapper.ReservationMapper;
import com.zzyl.nursing.domain.Reservation;
import com.zzyl.nursing.service.IReservationService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import java.util.Arrays;
/**
 * 预约信息Service业务层处理
 * 
 * @author zhengyh
 * @date 2025-03-02
 */
@Service
public class ReservationServiceImpl extends ServiceImpl<ReservationMapper,Reservation> implements IReservationService
{
    @Autowired
    private ReservationMapper reservationMapper;

    /**
     * 查询预约信息
     * 
     * @param id 预约信息主键
     * @return 预约信息
     */
    @Override
    public Reservation selectReservationById(Long id)
    {
        return getById(id);
    }

    /**
     * 分页查询预约信息
     * 
     * @param status 预约信息
     * @return 预约信息
     */
    @Override
    public List<Reservation> selectReservationList(Integer status)
    {
        return reservationMapper.selectReservationList(status);
    }

    /**
     * 新增预约信息
     * 
     * @param reservation 预约信息
     * @return 结果
     */
    @Override
    public int insertReservation(Reservation reservation)
    {
        return save(reservation)?1:0;
    }

    /**
     * 修改预约信息
     * 
     * @param reservation 预约信息
     * @return 结果
     */
    @Override
    public int updateReservation(Reservation reservation)
    {
        reservation.setUpdateBy(UserThreadLocal.getUserId().toString());
        return updateById(reservation)?1:0;
    }

    /**
     * 批量删除预约信息
     * 
     * @param ids 需要删除的预约信息主键
     * @return 结果
     */
    @Override
    public int deleteReservationByIds(Long[] ids)
    {
        return removeByIds(Arrays.asList(ids))?1:0;
    }

    /**
     * 删除预约信息信息
     * 
     * @param id 预约信息主键
     * @return 结果
     */
    @Override
    public int deleteReservationById(Long id)
    {
        return removeById(id)?1:0;
    }

    /**
     * 查询当天取消预约数量
     *
     * @return
     */
    @Override
    public Integer getCancelledCount() {
        // 获取当前登录用户id
        Long userId = UserThreadLocal.getUserId();

        // 获取当天的开始时间和结束时间
        long time = System.currentTimeMillis();
        LocalDateTime ldt = LocalDateTimeUtil.of(time);
        LocalDateTime startTime = ldt.toLocalDate().atStartOfDay();
        LocalDateTime endTime = startTime.plusDays(1);

        // 查询当天的取消预约数量
        Integer count = reservationMapper.getCancelledCount(userId, startTime, endTime);

        return count;
    }

    /**
     * 先将时间戳转换为字符串，再将字符串转换为 LocalDateTime 类型
     * @param timestamp 时间戳（毫秒）
     * @return 对应的 LocalDateTime 对象
     */
    public static LocalDateTime timestampToLocalDateTime(Long timestamp) {
        // 定义日期时间格式
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        // 获取上海时区
        ZoneId shanghaiZone = ZoneId.of("Asia/Shanghai");
        // 将时间戳转换为 Instant 对象
        Instant instant = Instant.ofEpochMilli(timestamp);
        // 将 Instant 对象转换为上海时区的 LocalDateTime 对象
        LocalDateTime localDateTime = instant.atZone(shanghaiZone).toLocalDateTime();
        // 格式化 LocalDateTime 对象为字符串
        String formattedDate = localDateTime.format(formatter);
        // 解析字符串为 LocalDateTime 对象
        return LocalDateTime.parse(formattedDate, formatter);
    }

    /**
     * 查询每个时间段剩余预约次数
     *
     * @param time
     * @return
     */
    @Override
    public List<TimeCountVo> countByTime(Long time) {
        LocalDateTime nowTime = timestampToLocalDateTime(time);

        LocalDateTime startTime = nowTime;
        LocalDateTime endTime = startTime.plusHours(24);
        return reservationMapper.countByTime(startTime, endTime);
    }

    /**
     * 更新预约状态
     */
    @Override
    public void updateReservationStatusJob() {
        LocalDateTime startTime = LocalDateTime.now();
        LambdaUpdateWrapper<Reservation> queryWrapper = new LambdaUpdateWrapper<>();
        queryWrapper.set(Reservation::getStatus, 3);
        queryWrapper.lt(Reservation::getTime, startTime);
        update(queryWrapper);
    }
}
